package net.zeus.sample.rabbitmq.topic;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;

import net.zeus.sample.rabbitmq.utils.ConnectionUtils;

/**
 * 将路由键和某模式匹配
 * # 匹配一个或多个
 * * 匹配一个
 * 
 * @author Michael
 *
 */
public class TopicSender {
	
	public static void addUser() {
		try {
			sendTopic("user.add");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public static void removeUser() {
		try {
			sendTopic("user.remove");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public static void sendTopic(String topicKey) throws Exception{
		Connection conn = ConnectionUtils.getConnection();
		
		Channel channel = conn.createChannel();

		channel.exchangeDeclare(ConnectionUtils.EXCHANGE_TOPIC_NAME, "topic");
		
		String msg = "exchange topic:" + topicKey;
		
		channel.basicPublish(ConnectionUtils.EXCHANGE_TOPIC_NAME, topicKey, null, msg.getBytes());
		
		System.out.println("send exchange topic msg:" + msg);
		
		channel.close();
		ConnectionUtils.closeConnection(conn);
	}

	public static void main(String[] args) throws Exception{
//		addUser();
		removeUser();
	}

}
